#pragma once

#include <string>
#include <map>
#include <vector>
#include "../Base/CriticalSection.h"

class ImportDataCache
{
public:
	struct Data
	{
		std::string field_index_;
		int pos_ = 0;
		int length_ = 0;

		Data()
			: field_index_(""), pos_(0), length_(0)
		{

		}

		Data(std::string field_index_, int pos, int length)
			: field_index_(field_index_), pos_(pos), length_(length)
		{

		}

		Data& operator=(const Data& data)
		{
			field_index_ = data.field_index_;
			pos_ = data.pos_;
			length_ = data.length_;

			return *this;
		}

        bool operator==(const Data& data)
        {
            return field_index_ == data.field_index_ && pos_ == data.pos_ && length_ == data.length_;
        }
	};

	typedef std::vector<Data> Datas;

	ImportDataCache();
	~ImportDataCache();

	void load();

    void addFaild(const char *table, Data &data, Data &pos);

    void removeFaild(const char *table, Data &data);

    void addPos(const char *table, Data &pos);

    const std::map<std::string, Datas> &faild_cache_datas() { return faild_cache_datas_; }

	const std::map<std::string, Data> &success_cache_pos() { return success_cache_pos_; }

protected:
    bool hasCacheData(const char *table);

    bool getFaild(const char *table, int index, Data &data);

    int countFaild(const char *table);
    
    int getSuccess(const char *table);

	void readFile(const char *file_name, Data &success_cache_pos, Datas& faild_datas);

    void saveFile(const char *table, Data success_cache_pos, const Datas& datas);

protected:
	std::map<std::string, Datas> faild_cache_datas_;

    std::map<std::string, Data> success_cache_pos_;
};

